/**
 * 
 */
package org.openwork.portlet.skills.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.openwork.portlet.action.BaseAction;
import org.openwork.portlet.skillmanagement.model.Skill;
import org.openwork.portlet.skillmanagement.model.SkillCategory;
import org.openwork.portlet.skillmanagement.model.SkillUser;
import org.openwork.portlet.skillmanagement.service.SkillCategoryLocalServiceUtil;
import org.openwork.portlet.skillmanagement.service.SkillLocalServiceUtil;
import org.openwork.portlet.skillmanagement.service.SkillUserLocalServiceUtil;
import org.openwork.portlet.skillmanagement.service.persistence.SkillUserPK;

import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;

/**
 * @author samuelgmartinez
 *
 */
public class ViewEditSkillUserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8133974014137315054L;

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = getServletRequest();
		String userIdParam = ParamUtil.get(request, WebKeys.USER_ID_PARAM, StringPool.BLANK);
		String skillIdParam = ParamUtil.get(request, WebKeys.SKILL_ID_PARAM, StringPool.BLANK);
		
		if(Validator.isNotNull(userIdParam) && Validator.isNotNull(skillIdParam) &&
			Validator.isNumber(userIdParam) && Validator.isNumber(skillIdParam)	) {
			SkillUser skillUser = SkillUserLocalServiceUtil.getSkillUser(new SkillUserPK(GetterUtil.getLong(userIdParam), GetterUtil.getLong(skillIdParam)) );
			request.setAttribute(WebKeys.SKILL_USER_ATTR, skillUser);
		}		
		
		long scopeGroupId = PortalUtil.getScopeGroupId(request);
		
		List<SkillCategory> skillCategories = SkillCategoryLocalServiceUtil.search(scopeGroupId);
		request.setAttribute(WebKeys.SKILL_CAT_LIST_ATTR, skillCategories);
		
		String skillCategoryId = ParamUtil.get(request, WebKeys.SKILL_CAT_ID_PARAM, StringPool.BLANK);
		if(Validator.isNotNull(skillCategoryId) && Validator.isNumber(skillCategoryId)) {
			List<Skill> skills = SkillLocalServiceUtil.searchByCategoryId(GetterUtil.getLong(skillCategoryId));
			request.setAttribute(WebKeys.SKILL_LIST_ATTR, skills);
		}
		
		return SUCCESS;
	}

}
